package cn.edu.controller;


import cn.edu.OMservice.roomService.OrgApacheOpenmeetingsWebserviceRoomWebService;
import cn.edu.OMservice.roomService.RoomWebService;
import cn.edu.OMservice.userwebService.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.rmi.RemoteException;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping(value="/op")
@Api(value="远程会诊",tags="op")
public class TeleDiagnosisController {
    private static RoomWebService roomService;
    private static UserWebService userService;
    private static Map<Long, Integer> roomCountersMap = new LinkedHashMap<>();

    static {
        OrgApacheOpenmeetingsWebserviceRoomWebService orgRoom = new OrgApacheOpenmeetingsWebserviceRoomWebService();
        OrgApacheOpenmeetingsWebserviceUserWebService orgUser = new OrgApacheOpenmeetingsWebserviceUserWebService();
        userService = orgUser.getUserService();
        roomService = orgRoom.getRoomWebServicePort();
    }

    @ApiOperation(value="老师加入公共房间")
    @RequestMapping(value="/addToRoom",method= RequestMethod.POST)
    public ServiceResult createRoom(String id, String fname, String lname, Long roomId) throws RemoteException {

        String sid = getSid("Jiangnan","Admin@123");
        //创建外部人员访问信息
        ExternalUserDTO user = new ExternalUserDTO();
        user.setEmail("460205875@qq.com");
        user.setExternalId(id);
        user.setExternalType("Doctor");
        user.setFirstname(fname);
        user.setLastname(lname);
        user.setLogin("T"+id);

        RoomOptionsDTO options = new RoomOptionsDTO();
        options.setRoomId(roomId);
        options.setShowAudioVideoTest(true);
        options.setModerator(true);


        ServiceResult serviceRs = userService.getRoomHash(sid, user, options);
        System.out.println(serviceRs.getMessage());

        roomCountersMap.putIfAbsent(roomId, 1);

        return serviceRs;
    }

    /**
     * 得到某个登录用户的sid
     */
    private String getSid(String name,String pwd) {
        ServiceResult rs = userService.login(name, pwd);
        return rs.getMessage();
    }
}